#include "caltoolwidget.h"

CalToolWidget::CalToolWidget(QWidget* parent): QWidget(parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout;
    createFunctionsBox();
    mainLayout->setAlignment(Qt::AlignTop);

    mainLayout->addWidget(algebraBox);
    mainLayout->addWidget(calculusBox);

    setLayout(mainLayout);
}
void CalToolWidget::createFunctionsBox()
{
    algebraBox = new QComboBox;
    connect(algebraBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(emitAlgebraFunction(QString)));
    algebraBox->addItem(tr("Đại số"));
    algebraBox->addItem(tr("Khai triển"),QVariant(tr("expand(%);")));
    algebraBox->addItem(tr("Rút gọn"), QVariant(tr("ratsimp(%);")));
    algebraBox->addItem(tr("Nhân tử chung"),QVariant(tr("factor(%);")));
    algebraBox->addItem(tr("Giải hệ phương trình"),QVariant(tr("solve([],[]);")));
    algebraBox->addItem(tr("ƯCLN hai đa thức"),QVariant(tr("gcd();")));
    algebraBox->addItem(tr("Chia đa thức"),QVariant(tr("divide();")));

    calculusBox = new QComboBox;
    connect(calculusBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(emitCalculusFunction(QString)));
    calculusBox->addItem(tr("Giải tích"));
    calculusBox->addItem(tr("Tích phân"),QVariant(tr("integrate();")));
    calculusBox->addItem(tr("Đạo hàm"),QVariant(tr("diff();")));
    calculusBox->addItem(tr("Giới hạn"),QVariant(tr("limit();")));
    calculusBox->addItem(tr("Tính tổng"),QVariant(tr("sum();")));
    calculusBox->addItem(tr("Tính tích"),QVariant(tr("product();")));
}

void CalToolWidget::emitAlgebraFunction(QString function)
{
    if(function != tr("Đại số"))
    {
        QString command = algebraBox->itemData(algebraBox->currentIndex()).toString();
        emit sendFunction(command);
        algebraBox->setCurrentIndex(0);
    }
}
void CalToolWidget::emitCalculusFunction(QString function)
{
    if(function != tr("Phép tính"))
    {
        QString command = calculusBox->itemData(calculusBox->currentIndex()).toString();
        emit sendFunction(command);
        calculusBox->setCurrentIndex(0);
    }
}
